package com.javasm.controller.MisMannageController;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.MisMannageEntity.BuyData;
import com.javasm.entity.MisMannageEntity.ReturnCode;
import com.javasm.entity.MisMannageEntity.ReturnEntity;
import com.javasm.service.MisMannageService.CountService;
import com.javasm.service.MisMannageService.impl.CountServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author:JAVASM
 * @description:
 * @date:2022/5/27 21:35
 * @version:0.1
 * @since:jdk11
 */
@WebServlet("/echarts/*")
public class EchartsServlet extends BaseServlet {

    public void everyDay(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //String queryDateStr = req.getParameter("queryDate");
        String queryDateStr ="2022-05-27";
        Date queryDate = null;
        if (queryDateStr!=null && !"".equals(queryDateStr)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                queryDate = sdf.parse(queryDateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        System.out.println(queryDate);

        CountService cs = new CountServiceImpl();
        List<BuyData> buyDataList = cs.buyFundData(queryDate);

        ReturnEntity re = new ReturnEntity();
        if (buyDataList.size() > 0) {
            re.setRetCode(ReturnCode.QUERY_OK.getCode());
            re.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            re.setRetData(buyDataList);
        } else {
            re.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            re.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();

    }

    public void everyMonth(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //String queryDateStr = req.getParameter("queryMonth");
        String queryMonth ="2022-05";

        CountService cs = new CountServiceImpl();
        List<BuyData> buyDataList = cs.buyMonthData(queryMonth);

        ReturnEntity re = new ReturnEntity();
        if (buyDataList.size() > 0) {
            re.setRetCode(ReturnCode.QUERY_OK.getCode());
            re.setRetMsg(ReturnCode.QUERY_OK.getMsg());
            re.setRetData(buyDataList);
        } else {
            re.setRetCode(ReturnCode.QUERY_FAILED.getCode());
            re.setRetMsg(ReturnCode.QUERY_FAILED.getMsg());
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(re));
        writer.flush();
        writer.close();

    }
}
